<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修编号" prop="reportId">
        <el-input
          v-model="queryParams.reportId"
          placeholder="请输入编号查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修人" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="请输入报修人查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态"  label-width="auto">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in dict.type.community_report_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>

        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:report:add']">新增</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="报修编号" align="center" prop="reportId"/>
      <el-table-column label="报修时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报修人" align="center" prop="reportName"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.community_report_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="维修工" align="center" prop="nickName"/>
      <el-table-column label="详情" align="center" prop="remark"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:report:edit']"
            v-if="scope.row.status != 2"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-custom"
            @click="handleAssign(scope.row)"
            v-hasPermi="['system:report:assgin']"
            v-if="scope.row.status != 2"
          >指派
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="handleFinish(scope.row)"
            v-hasPermi="['system:report:finish']"
            v-if="scope.row.status != 2 && scope.row.status != 0"
          >完成
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetails(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:report:remove']"
            v-if="scope.row.status != 2 && scope.row.status != 1"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--  添加修改对话框  -->
    <add-and-update
      :visible.sync="addReportVisible"
      :form="form" :title="title"
      :getList="getList"
      :reset="reset"
    />
    <!-- 指派对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               width="500px"
               append-to-body
               :before-close="cancel"
    >
      <el-form ref="form" :model="from" :rules="rules" label-width="80px">

        <el-form-item label="维修工" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择维修工">
            <el-option
              v-for="item in reportOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title"
               :visible.sync="detailsVisible"
               width="500px"
               append-to-body
               :before-close="cancel"
    >
      <el-row :gutter="20">
        <el-col :span="10"><div class="grid-content bg-purple">报修人姓名：{{ reportName }}</div></el-col>
        <el-col :span="10"><div class="grid-content bg-purple">报修人电话：{{ phone }}</div></el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10"><div class="grid-content bg-purple">地址：{{ address }}</div></el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10"><div class="grid-content bg-purple">报修内容：{{ reportRemark }}</div></el-col>
      </el-row>
      <div>{{ reportFinshDate }}</div>
      <div>维修完成时间</div>
      <el-steps :space="50" direction="vertical" :active="1" style="margin: 20px 0px">

        <el-step
          v-for="(step, index) in formDetails"
          :key="index"
          :title="step.detailsCreateTime"
          :description="step.detailsRemark"
        ></el-step>
      </el-steps>
      <div>{{ reportDate }}</div>
      <div>{{ reportName }}申请报修</div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReport,
  addReportUserId,
  getReport,
  delReport,
  updateReportUserId,
  updateReportFinish
} from "@/api/system/report";
import addAndUpdate from "./addAndUpdate";
import {listReporter} from "@/api/system/user";
import log from "@/views/monitor/job/log";

export default {
  name: "Report",
  dicts: [`community_report_status`],
  components: {addAndUpdate},
  data() {
    return {
      reportName: null,
      phone: null,
      address: null,
      reportRemark: null,
      reportDate: null,
      detailsRemark: null,
      reportFinshDate: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // report表格数据
      reportList: [],
      // 部门树选项
      deptOptions: [{
        value: 'deptId',             // ID字段名
        label: 'deptName',         // 显示名称
        children: 'children'    // 子级字段名
      }],
      userOptions: [],
      // 维修工列表
      reportOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示新增修改弹出层
      addReportVisible: false,
      // 是否显示详情弹出层
      detailsVisible: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        reportId: null,
        pageNum: 1,
        pageSize: 10,
        reportName: null,
        phone: null,
        address: null,
        reportDate: null,
        userId: null,
        status: null,
      },
      form: {
      },
      deptList: [],
      userList: [],
      // 表单参数
      from: {

      },
      // 报修详情表
      formDetails: [],
      // 表单校验
      rules: {
        deptId: [
          {required: true, message: "上级部门不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态(0:未指派，1：待处理，2：已完成)不能为空", trigger: "change"}
        ],
      }
    };
  },
  async created() {
    await this.getList();
    const reportRes = await listReporter();
    this.reportOptions = reportRes.rows

  },
  methods: {
    /** 查询report列表 */
    async getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        // console.log("-------------", this.reportList)
        // console.log("表格时间",this.reportList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailsVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: null,
        reportName: null,
        phone: null,
        address: null,
        reportDate: null,
        userId: null,
        remark: null,
        status: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */

    handleQuery() {
      debugger
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addReportVisible = true;
      this.title = "添加报修单";
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      // 赋值的过程
      this.reportName = row.reportName;
      this.phone = row.phone;
      this.address = row.address;
      this.reportRemark = row.remark;
      this.reportDate = row.createTime
      this.reportFinshDate = row.updateTime
      this.detailsRemark = row.detailsRemark
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.formDetails = response.data;
        this.detailsVisible = true;
        this.title = "报修详情单";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // debugger
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data[0];
        console.log(this.form)
        this.addReportVisible = true;
        this.title = "修改report";
      });
    },
    /** 指派按钮 */
    handleAssign(row) {
      this.reset();
      this.form.reportId = row.reportId
      this.form.userId = Number(row.userId)
      this.form.status = row.status
      this.open = true;
      this.title = "选择维修工";
    },
    /** 完成按钮  */
    handleFinish(row) {
      this.reset();
      this.form.reportId = row.reportId
      this.form.status = row.status
      updateReportFinish({reportId: this.form.reportId, status: this.form.status}).then(response => {
        this.$modal.msgSuccess("已完成");
        this.getList();
      })
    },
    /** 指派提交按钮 */
    submitForm: function (data) {
      updateReportUserId({
        userId: this.form.userId,
        reportId: this.form.reportId,
        status: this.form.status
      }).then(response => {
        this.$modal.msgSuccess("指派成功");
        this.open = false;
        this.getList();
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除report编号为"' + reportIds + '"的数据项？').then(function () {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-row {
  margin-bottom: 10px;
&:last-child {
   margin-bottom: 0;
 }
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  margin-top: 20px;
  border-radius: 4px;
  min-height: 18px;
}
.row-bg {
  padding: 5px 0;
}
</style>
